package operation;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class ModifOperationJDBC implements IOperation{
    @Override
    public void work() throws SQLException {
        System.out.println("修改学生消息");
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("zkzsa123");

        Connection connection = dataSource.getConnection();
        String sql = "update student set studentName = ?,age = ?,sex = ?,id = ?,telephone = ? where studentId = ?";
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要修改学生的学号");
        String studentId = scanner.next();
        System.out.println("请输入你要修改的姓名");
        String studentName = scanner.next();
        System.out.println("请输入你要修该的年龄");
        int age = scanner.nextInt();
        System.out.println("请输入你要修改的性别");
        String sex = scanner.next();
        System.out.println("请输入你要修改的身份证");
        String id = scanner.next();
        System.out.println("请输入你要修改的电话");
        String telephone = scanner.next();
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setObject(1,studentName);
        statement.setObject(2,age);
        statement.setObject(3,sex);
        statement.setObject(4,id);
        statement.setObject(5,telephone);
        statement.setObject(6,studentId);
        int n = statement.executeUpdate();
        statement.close();
        connection.close();

        if(n > 0){
            System.out.println("修改成功");
            return;
        }
    }
}
